﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EM.Repository.utility;
using System.Data.SqlClient;
using System.Data;

namespace EM.operation.paymentplan
{
    public partial class default_edit : System.Web.UI.Page
    {
        string sTask = "";
        public int INCOMEID = 0;
        FillCombo Fc = new FillCombo();
        Conversion oC = new Conversion();
        protected void Page_Load(object sender, EventArgs e)
        {

            Authentication ua = new Authentication();
            ua.CheckRolePageAuthentication(18, Authentication.RedirectionURLTypes.TimeOutPage);


            sTask = Request.QueryString["task"].ToString();
            Int32.TryParse(Request.QueryString["ID"], out INCOMEID);

            if ((bool)HttpContext.Current.Session["UserIsAnAdmin"])
                ddlUser.Enabled = true;
            else
                ddlUser.Enabled = false;

            if (!IsPostBack)
            {
                switch (sTask)
                {


                    case "Insert":
                        {
                            btnSave.CommandArgument = "Insert";
                            Fc.IncomeType(ddlExpenseType, 0, true,  Convert.ToInt32(Session["WORKGROUPID"]));
                            Fc.User(ddlUser, "0", true, Convert.ToInt32(Session["WORKGROUPID"]));
                            Fc.Currency(ddlCurrencyId, "1", true);
                            break;
                        }
                    case "Update":
                        {
                            btnSave.CommandArgument = "Update";
                            GetData();

                            break;
                        }

                    case "Delete":
                        {
                            btnSave.CommandArgument = "Delete";
                            lblSave.Text = "Sil";
                            GetData();
                            break;
                        }
                }
            }
        }

        private void GetData()
        {

            DbHelper srv = new DbHelper();
            SqlCommand cmd = new SqlCommand();

            string sSQL = @" SELECT PAYMENTPLANID,PAYMENTTYPEID,AMOUNT,CURRENCYID,DESCRIPTION,PLANNEDPAYMENTDATE,STATUS,USERID
                                STATUS FROM TBLPAYMENTPLAN WHERE PAYMENTPLANID = @PAYMENTPLANID";
            cmd.CommandText = sSQL;
            cmd.Parameters.Add("INCOMEID", SqlDbType.Int).Value = INCOMEID;
            DataTable dt = srv.GetDataTable(cmd, "");

            if (dt.Rows.Count > 0)
            {
                lblExpenseId.Text = dt.Rows[0]["INCOMEID"].ToString();
                rdpDate.SelectedDate = Convert.ToDateTime(dt.Rows[0]["INCOMEDATE"]);
                Fc.IncomeType(ddlExpenseType, Convert.ToInt32(dt.Rows[0]["INCOMETYPEID"]), true,  Convert.ToInt32(Session["WORKGROUPID"]));
                Fc.Currency(ddlCurrencyId, dt.Rows[0]["CURRENCYID"].ToString(), true);
                txtAmount.Text = oC.CNumS2C(Convert.ToDouble(dt.Rows[0]["AMOUNT"]));
                txtDescription.Text = dt.Rows[0]["DESCRIPTION"].ToString();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {

            DbHelper srv = new DbHelper();
            SqlCommand cmd = new SqlCommand();

            string sSQL = "";

            if (sTask == "Insert")
            {
                sSQL = @" 
                        DECLARE @UID INT;
                        INSERT INTO TBLINCOME
                        (INCOMETYPEID,INCOMEDATE,AMOUNT,DESCRIPTION,CREUSERID,CREDATE,MODUSERID,MODDATE, INCOMEUSERID, CURRENCYID)
                        SELECT @INCOMETYPEID,@INCOMEDATE,@AMOUNT,@DESCRIPTION,@CREUSERID,GETDATE(),@CREUSERID,GETDATE(),@INCOMEUSERID,@CURRENCYID
                        SET @UID = SCOPE_IDENTITY();
                        SELECT @UID;";
            }
            else if (sTask == "Update")
            {
                sSQL = @"UPDATE TBLINCOME SET
                        INCOMETYPEID = @INCOMETYPEID,
                        INCOMEDATE = @INCOMEDATE,
                        AMOUNT = @AMOUNT,
                        DESCRIPTION = @DESCRIPTION,
                        MODUSERID = @CREUSERID,
                        INCOMEUSERID = @INCOMEUSERID,
                        MODDATE = GETDATE(),
                        CURRENCYID = @CURRENCYID
                        WHERE INCOMEID = @INCOMEID
                         ";
            }
            else if (sTask == "Delete")
            {
                sSQL = " DELETE FROM TBLINCOME WHERE INCOMEID = @INCOMEID ";
            }

            cmd.CommandText = sSQL;
            cmd.Parameters.AddWithValue("@INCOMEID", INCOMEID);
            cmd.Parameters.AddWithValue("@CREUSERID", Session["USERID"]);
            cmd.Parameters.AddWithValue("@INCOMEDATE", rdpDate.SelectedDate);
            cmd.Parameters.AddWithValue("@DESCRIPTION", txtDescription.Text);
            cmd.Parameters.AddWithValue("@AMOUNT", oC.CNumC2S(txtAmount.Text));
            cmd.Parameters.AddWithValue("@INCOMETYPEID", ddlExpenseType.SelectedValue);
            cmd.Parameters.AddWithValue("@CURRENCYID", ddlCurrencyId.SelectedValue);

            if ((bool)HttpContext.Current.Session["UserIsAnAdmin"])
                cmd.Parameters.AddWithValue("@INCOMEUSERID", ddlUser.SelectedValue);
            else
                cmd.Parameters.AddWithValue("@INCOMEUSERID", Session["USERID"]);

            DataTable dt = srv.GetDataTable(cmd, "");

            if (sTask == "Insert")
            {
                if (srv.GetGlobalDefaultValue("ENABLE_INCOME_NOTIFICATION") == "Y")
                {
                    Notice nc = new Notice();
                    nc.IncomeNotification(Convert.ToInt32(dt.Rows[0][0]), Convert.ToInt32(Session["WORKGROUPID"]));
                }
            }

            string strScript = "window.opener['rgMaster'].AjaxRequest('rgMaster', 'Rebind'); window.close();";
            ClientScript.RegisterStartupScript(typeof(string), "", strScript.ToString(), true);
        }
    }
}